package com.eques.demo.lock

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import com.eques.base.BaseActivity
import com.eques.database.dao.DevListDao
import com.eques.database.dao.ShaowDao
import com.eques.demo.databinding.ActivityLockSettingBinding
import com.eques.event.EventTotalThread
import com.eques.icvss.utils.ELog
import com.eques.service.DoorBellService
import com.eques.util.BaseAppApplication
import com.eques.util.Config
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LockSettingActivity : BaseActivity() {
    private lateinit var devDao: DevListDao
    private lateinit var shaowDao: ShaowDao
    private var devId: String = ""
    private lateinit var userName: String
    private var workMode = 0//工作模式
    private lateinit var binding: ActivityLockSettingBinding
    private var devState=0;//设备状态 0:离线 1:在线 2:休眠
    private var num:String=""
    private var updateState=0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLockSettingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        EventBus.getDefault().register(this)
        initData()
        setupClickListeners()
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    @SuppressLint("NotifyDataSetChanged")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onRefreshEvent(event: EventTotalThread) {
        when (event.refresh) {
            Config.EVENT_DEVICE_LIST_REFRESH, Config.EVENT_BDY_LIST->{
                initDevInfo()
            }
        }
    }

    private fun getIntentData() {
        devId = intent.getStringExtra("devId").toString()
        userName = preference.getString(Config.EQUES_USER_NAME)!!
    }

    private fun initDevInfo() {
        devDao = BaseAppApplication.Companion.instance!!.getYkDao(this)
        CoroutineScope(Dispatchers.IO).launch {
            val devList = devDao.getBdyListEntry(userName, devId)
            ELog.e("设备详细信息", devList)
            if (devList != null) {
                runOnUiThread {
                    devState=devList.stat
                    binding.tvDevNick.text =
                        if (devList.nick == "") devList.defaultNick else devList.nick
                    binding.tvDevMac.text = devList.name
                    updateState=devList.remoteupg
                    if(updateState>=1){
                        binding.tvUpgradeStatus.text = "设备可升级"
                    }else{
                        binding.tvUpgradeStatus.text = "已是最新版本"
                    }
                }

            }
        }

    }

    private fun initDevShaowInfo() {
        shaowDao = BaseAppApplication.Companion.instance!!.getShaowDao(this)
        CoroutineScope(Dispatchers.IO).launch {
            val shaowList = shaowDao.getShaowEntry(userName, devId)
            ELog.e("设备影子详细信息", shaowList)
            if (shaowList != null) {
                runOnUiThread {
                    workMode = shaowList.work_mode.toInt()
                    num=shaowList.vnum
                    binding.tvCurrentVersion.text = "当前设备版本：$num"
                }

            }
        }
    }

    private fun setupClickListeners() {
        // 功能设置点击事件
        binding.remoteUnlock.setOnClickListener {
            showToast("远程开锁")
        }

        binding.monitorMode.setOnClickListener {
            val intent = Intent(this, WorkModeActivity::class.java)
            intent.putExtra("devId", devId)
            startActivity(intent)
        }


        binding.screenTime.setOnClickListener {
            val intent = Intent(this, ScreenTimeSettingActivity::class.java)
            intent.putExtra("devId", devId)
            startActivity(intent)
        }




        binding.systemUpgrade.setOnClickListener {
            //升级前判断设备状态，设备状态0 不能升级 2 发唤醒设备上线后升级  1直接发送下面的指令
            DoorBellService.icvss!!.equesUpgradeDevice(devId)
        }

        // 删除设备点击事件
        binding.deleteDevice.setOnClickListener {
            showDeleteDeviceDialog()
        }
    }


    override fun initView() {
    }

    override fun initData() {
        getIntentData()
        initDevInfo()
        initDevShaowInfo()
    }

    private fun showDeleteDeviceDialog() {
        // 这里可以显示删除设备的确认对话框
        AlertDialog.Builder(this)
            .setTitle("删除设备")
            .setMessage("确定要删除此设备吗？")
            .setPositiveButton("确定") { dialog, which ->
                // 执行删除设备的逻辑
                DoorBellService.icvss!!.equesDelDevice(devId)
                finish()
            }
            .setNegativeButton("取消", null)
            .show()
    }
}